﻿using LSL.SqlClient;
using System;
using System.Web;
using System.Web.SessionState;
using Liu_Project.Utils;

namespace Liu_Project.Web.Admin
{
    public abstract class HandlerBaseVirtual : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable => throw new NotImplementedException();
        
        public string action = "";
        public HttpContext _context;
        public string ModuleId = "";
        public int page = 1;
        public int pageSize = 10;
        public int total = 0;

        /// <summary>
        /// 获取用户信息,返回NULL则SESSION过期
        /// </summary>
        public Entity.Admin.EntUser CurrentUser
        {
            get
            {
                var currentUser = System.Web.HttpContext.Current.Session["CurrentUser"];
                if (currentUser != null)
                {
                    return (Entity.Admin.EntUser)currentUser;
                }
                else
                {
                    return null;
                }
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            if (context.Session["CurrentUser"] == null)
            {
                context.Response.Write(CommonFunction.ErrorJson("用户信息过期，请重新登录!", 101));
                context.Response.End();
                return;
            }

            action = DataConvert.ToString(context.Request["act"], "");
            if (string.IsNullOrWhiteSpace(action))
            {
                context.Response.Write(CommonFunction.ErrorJson("方法名不能为空!"));
                context.Response.End();
                return;
            }
            
            ModuleId = DataConvert.ToString(context.Request["ModuleId"], "");
            page = DataConvert.ToInt(context.Request["page"]);
            pageSize = DataConvert.ToInt(context.Request["limit"]);

            _context = context;

            ProccessAction();
        }

        //具体指令对应方法
        public virtual void ProccessAction()
        {
            switch (action.ToLower())
            {
                case Action.add:
                    Add();
                    break;
                case Action.loadpage:
                    LoadPage();
                    break;
                case Action.delete:
                    Delete();
                    break;
                case Action.query:
                    Query();
                    break;
                case Action.querybyid:
                    QueryById();
                    break;
                case Action.update:
                    Update();
                    break;
                case Action.loadlist:
                    LoadList();
                    break;
                case Action.initdata:
                    InitData();
                    break;
                case Action.getloadpages:
                    GetLoadPages();
                    break;
                case Action.logout:
                    Logout();
                    break;
                case Action.save:
                    Save();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 加载分页列表
        /// </summary>
        public virtual void LoadPage() { }

        /// <summary>
        /// 新增方法
        /// </summary>
        public virtual void Add() { }

        /// <summary>
        /// 更新方法
        /// </summary>
        public virtual void Update() { }

        /// <summary>
        /// 保存方法
        /// </summary>
        public virtual void Save() { }

        /// <summary>
        /// 删除方法
        /// </summary>
        public virtual void Delete() { }

        /// <summary>
        /// 查询方法
        /// </summary>
        public virtual void Query() { }

        /// <summary>
        /// 查询Id方法
        /// </summary>
        public virtual void QueryById() { }

        /// <summary>
        /// 登录方法
        /// </summary>
        public virtual void Login() { }

        /// <summary>
        /// 加载数据
        /// </summary>
        public virtual void LoadList() { }

        /// <summary>
        /// 页面初始化读取数据
        /// </summary>
        public virtual void InitData() { }

        /// <summary>
        /// 获取绑定到页面的数据
        /// </summary>
        public virtual void GetLoadPages() { }

        /// <summary>
        /// 退出系统
        /// </summary>
        public virtual void Logout() { }

    }

    /// <summary>
    /// 动作指令
    /// </summary>
    public class Action
    {
        /// <summary>
        /// 分页列表操作
        /// </summary>
        public const string loadpage = "loadpage";

        /// <summary>
        /// 新增操作
        /// </summary>
        public const string add = "add";

        /// <summary>
        /// 更新操作
        /// </summary>
        public const string update = "update";

        /// <summary>
        /// 保存操作
        /// </summary>
        public const string save = "save";

        /// <summary>
        /// 删除操作
        /// </summary>
        public const string delete = "delete";

        /// <summary>
        /// 查询操作
        /// </summary>
        public const string query = "query";

        /// <summary>
        /// 查询id操作
        /// </summary>
        public const string querybyid = "querybyid";

        /// <summary>
        /// 登录
        /// </summary>
        public const string login = "login";

        /// <summary>
        /// 加载数据
        /// </summary>
        public const string loadlist = "loadlist";

        /// <summary>
        /// 退出系统
        /// </summary>
        public const string logout = "logout";

        /// <summary>
        ///  页面初始化读取数据
        /// </summary>
        public const string initdata = "initdata";

        /// <summary>
        /// 获取页面绑定要绑定的数据
        /// </summary>
        public const string getloadpages = "getloadpages";
    }
}